//
//  UserMessageVC.m
//  jinwang
//
//  Created by jzb on 2018/3/24.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "UserMessageVC.h"
#import "UserMessageCell.h"
#import "UserMessageModel.h"
#import "BusinessCardVC.h"
#import "JWFindUserBoJianVC.h"


@interface UserMessageVC ()

@property (nonatomic,strong) UserMessageCell *cell;
@property (nonatomic,strong) UserMessageModel *Usermodel;

@end

@implementation UserMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"用户信息";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpTableView];
    
    if ([_VC_Type isEqualToString:@"good_friend"]) {
        
        [self makeUrl_Friends_information];
    }else{
        
        [self makeUrl_batchGetUserQrcode];
    }
    

}
//配置tableView
-(void)setUpTableView{
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([UserMessageCell class])]];
    
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[UserMessageCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.GuanZhuBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.ZhuanFaBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.PhoneBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.LookMingPianBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.LookShopBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.LookDongTaiBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.SendMessageBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    
    if ([_VC_Type isEqualToString:@"good_friend"]) {
        
        self.cell.AddFriendBtn.hidden = YES;
    }else{
        
        self.cell.AddFriendBtn.hidden = NO;
        [self.cell.AddFriendBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    self.cell.model = _Usermodel;
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 580;
}
- (void)cellButtonOnclikc:(UIButton *)sender{
    
    if (sender.tag == 1000) {
       //关注
        [self makeUrl_Dynamic_addFollow:sender];
        
        
        
    }else if (sender.tag == 1001){
        
        
    }else if (sender.tag == 1002){
        //电话
        if (_Usermodel.user_phone.length > 0) {
            
            NSMutableString * phoneStr = [[NSMutableString alloc] initWithFormat:@"tel://%@",_Usermodel.user_phone];
            
            NSURL *phoneUrl = [NSURL URLWithString:phoneStr];
            
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                
                [[UIApplication sharedApplication] openURL:phoneUrl];
                
            });
        }
        
    }else if (sender.tag == 1003){
        //查看名片
        BusinessCardVC *vc = [[BusinessCardVC alloc]init];
        
        vc.model = _Usermodel;
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (sender.tag == 1004){
      
        
    }else if (sender.tag == 1005){
        //查看动态
        JWFindUserBoJianVC *vc = [JWFindUserBoJianVC new];
        vc.title = [NSString stringWithFormat:@"%@的博见",_Usermodel.user_nickname];
        vc.user_id = [_Usermodel.user_id stringValue];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (sender.tag == 1006){
        
        
    }else if (sender.tag == 1007){
        
        [self makeUrl_batchAddFriends];
        
    }
}
#pragma mark -网络请求
//扫码获取用户信息
-(void)makeUrl_batchGetUserQrcode{
    
    [[DDNetworkManagerDate makeUrl_batchGetUserQrcode_with_token:k_user_token user_id:_user_id] post_RequestFinshSuccess:^(id responseObject) {
//        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            _Usermodel = [UserMessageModel yy_modelWithDictionary:data];
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//好友资料
-(void)makeUrl_Friends_information{
    [[DDNetworkManagerDate makeUrl_Friends_informationWithToken:k_user_token user_id:_user_id] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            _Usermodel = [UserMessageModel yy_modelWithDictionary:data];
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//关注
-(void)makeUrl_Dynamic_addFollow:(UIButton *)sender{
    [[DDNetworkManagerDate makeUrl_Dynamic_addFollowWithToken:k_user_token user_id:_user_id] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            sender.userInteractionEnabled=NO;
            [sender setTitle:@"已关注" forState:UIControlStateNormal];
            sender.backgroundColor = [UIColor colorWithRed:238/255.0 green:167/255.0 blue:43/255.0 alpha:0.7];
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//添加好友
-(void)makeUrl_batchAddFriends{
    
    [[DDNetworkManagerDate makeUrl_batchAddFriends_with_token:k_user_token user_id:_user_id] post_RequestFinshSuccess:^(id responseObject) {
//        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
